﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Xml;

namespace FantanstPlatform.Decision.CaseXmlService
{
    public class XmlFactory
    {
        private string Path = ConfigurationManager.AppSettings["Path"];
        /// <summary>
        /// 创建对比柱状图Xml
        /// </summary>
        public void CreatMultiBarChartXml(List<CaseCount> caseCount, string title)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement chartnode = xmlDoc.CreateElement("chart");
            chartnode.SetAttribute("caption", title);
            chartnode.SetAttribute("shownames", "1");
            chartnode.SetAttribute("showvalues", "0");
            chartnode.SetAttribute("decimals", "0");
            chartnode.SetAttribute("numberPrefix", "");

            XmlElement categoriesNode = xmlDoc.CreateElement("categories");
            XmlElement datasetNode1 = xmlDoc.CreateElement("dataset");
            datasetNode1.SetAttribute("seriesName", "刑事案件");
            datasetNode1.SetAttribute("color", "AFD8F8");
            datasetNode1.SetAttribute("showValues", "0");

            XmlElement datasetNode2 = xmlDoc.CreateElement("dataset");
            datasetNode2.SetAttribute("seriesName", "治安案件");
            datasetNode2.SetAttribute("color", "8BBA00");
            datasetNode2.SetAttribute("showValues", "0");

            for (int i = 0; i < caseCount.Count; i++)
            {
                XmlElement categorynode = xmlDoc.CreateElement("category");
                categorynode.SetAttribute("label", caseCount[i].Station);
                categoriesNode.AppendChild(categorynode);

                XmlElement set1 = xmlDoc.CreateElement("set");
                set1.SetAttribute("value", caseCount[i].CriminalCount.ToString());
                datasetNode1.AppendChild(set1);

                XmlElement set2 = xmlDoc.CreateElement("set");
                set2.SetAttribute("value", caseCount[i].SecurityCount.ToString());
                datasetNode2.AppendChild(set2);
            }
            chartnode.AppendChild(categoriesNode);
            chartnode.AppendChild(datasetNode1);
            chartnode.AppendChild(datasetNode2);

            xmlDoc.AppendChild(chartnode);

            xmlDoc.Save(Path + title + ".xml");
        }
        /// <summary>
        /// 创建多线对比图Xml
        /// </summary>
        public void CreatMultiTendencyChartXml(List<ContrastCaseCount> caseCount, string title, string type)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement chartnode = xmlDoc.CreateElement("chart");
            chartnode.SetAttribute("caption", title);
            chartnode.SetAttribute("subcaption", "");
            chartnode.SetAttribute("lineThickness", "1");
            chartnode.SetAttribute("showValues", "0");
            chartnode.SetAttribute("formatNumberScale", "0");
            chartnode.SetAttribute("anchorRadius", "2");
            chartnode.SetAttribute("divLineAlpha", "20");
            chartnode.SetAttribute("divLineColor", "CC3300");
            chartnode.SetAttribute("divLineIsDashed", "1");
            chartnode.SetAttribute("showAlternateHGridColor", "1");
            chartnode.SetAttribute("alternateHGridAlpha", "5");
            chartnode.SetAttribute("alternateHGridColor", "CC3300");
            chartnode.SetAttribute("shadowAlpha", "40");
            chartnode.SetAttribute("divLineColor", "CC3300");
            chartnode.SetAttribute("labelStep", "2");
            chartnode.SetAttribute("numvdivlines", "5");
            chartnode.SetAttribute("chartRightMargin", "35");
            chartnode.SetAttribute("bgColor", "FFFFFF,CC3300");
            chartnode.SetAttribute("bgAngle", "270");
            chartnode.SetAttribute("bgAlpha", "10,10");

            XmlElement categoriesNode = xmlDoc.CreateElement("categories");

            XmlElement datasetNode1 = xmlDoc.CreateElement("dataset");
            datasetNode1.SetAttribute("color", "1D8BD1");
            datasetNode1.SetAttribute("anchorBorderColor", "1D8BD1");
            datasetNode1.SetAttribute("anchorBgColor", "1D8BD1");

            XmlElement datasetNode2 = xmlDoc.CreateElement("dataset");
            datasetNode2.SetAttribute("color", "F1683C");
            datasetNode2.SetAttribute("anchorBorderColor", "F1683C");
            datasetNode2.SetAttribute("anchorBgColor", "F1683C");

            XmlElement datasetNode3 = xmlDoc.CreateElement("dataset");
            XmlElement datasetNode4 = xmlDoc.CreateElement("dataset");
            XmlElement datasetNode5 = xmlDoc.CreateElement("dataset");
            if (type == "01")
            {
                datasetNode1.SetAttribute("seriesName", "刑事案件");
                datasetNode2.SetAttribute("seriesName", "治安案件");
            }
            else 
            {
                datasetNode1.SetAttribute("seriesName", "盗窃汽车案");
                datasetNode2.SetAttribute("seriesName", "诈骗案");

                datasetNode3.SetAttribute("seriesName", "抢劫案");
                datasetNode3.SetAttribute("color", "2AD62A");
                datasetNode3.SetAttribute("anchorBorderColor", "2AD62A");
                datasetNode3.SetAttribute("anchorBgColor", "2AD62A");

                datasetNode4.SetAttribute("seriesName", "抢夺案");
                datasetNode3.SetAttribute("color", "DBDC25");
                datasetNode4.SetAttribute("anchorBorderColor", "DBDC25");
                datasetNode4.SetAttribute("anchorBgColor", "DBDC25");

                datasetNode5.SetAttribute("seriesName", "入室盗窃案");
                datasetNode3.SetAttribute("color", "8BBA00");
                datasetNode5.SetAttribute("anchorBorderColor", "8BBA00");
                datasetNode5.SetAttribute("anchorBgColor", "8BBA00");
            }            

            for (int i = 0; i < caseCount.Count; i++)
            {
                XmlElement categorynode = xmlDoc.CreateElement("category");
                categorynode.SetAttribute("label", caseCount[i].Time);
                categoriesNode.AppendChild(categorynode);

                XmlElement set1 = xmlDoc.CreateElement("set");
                set1.SetAttribute("value", caseCount[i].CaseCount1);
                datasetNode1.AppendChild(set1);

                XmlElement set2 = xmlDoc.CreateElement("set");
                set2.SetAttribute("value", caseCount[i].CaseCount2);
                datasetNode2.AppendChild(set2);

                XmlElement set3 = xmlDoc.CreateElement("set");
                set3.SetAttribute("value", caseCount[i].CaseCount3);
                datasetNode3.AppendChild(set3);

                XmlElement set4 = xmlDoc.CreateElement("set");
                set4.SetAttribute("value", caseCount[i].CaseCount4);
                datasetNode4.AppendChild(set4);

                XmlElement set5 = xmlDoc.CreateElement("set");
                set1.SetAttribute("value", caseCount[i].CaseCount5);
                datasetNode5.AppendChild(set5);
            }
            chartnode.AppendChild(categoriesNode);
            chartnode.AppendChild(datasetNode1);
            chartnode.AppendChild(datasetNode2);
            if (type != "01")
            {
                chartnode.AppendChild(datasetNode3);
                chartnode.AppendChild(datasetNode4);
                chartnode.AppendChild(datasetNode5);
            }

            xmlDoc.AppendChild(chartnode);

            xmlDoc.Save(Path + title + ".xml");
        }
    }
}
